<template>
  <div>
    <el-table
      v-on="$listeners"
      :size="size"
      :tree-props="tp"
      :cols="cols"
      :row-key="rowKey"
      :data="data"
      border
      v-loading="loading"
    >
      <el-table-column
        v-on="$listeners"
        v-for="(item, index) in cols"
        :key="index"
        :label="item.title"
        :type="item.type"
        :prop="item.name"
        :width="item.width"
        :align="item.align"
      >
        <template v-if="item.slot" v-slot="{ row, index }">
          <slot :row="row" :index="index" :name="item.slot"></slot>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="value"
      @size-change="(v) => change('pagesize', v)"
      @current-change="(v) => change('pagenum', v)"
      :current-page="value.pagenum"
      :page-sizes="[10, 20, 30, 40, 50]"
      :page-size="value.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "Table",
  methods: {
    /**
     * 分页每页变更条数
     */
    change(key, value) {
      console.log(key, value);
      const obj = { ...this.value, [key]: value };
      this.$emit("input", obj);
      this.$emit("change", obj);
    },
  },
  props: {
    size: String,
    /**
     * 嵌套配置项
     */
    tp: {
      type: Object,
      default: () => ({ hasChildren: "hasChildren", children: "children" }),
    },
    /**
     * 分页
     */
    value: {
      type: Object,
      default: () => {},
    },
    /**
     * 总条数
     */
    total: Number,
    /**
     * rowKey
     */
    rowKey: {
      type: String,
      default: "id",
    },
    /**
     * loading 加载
     */
    loading: Boolean,
    /**
     * 数据列表
     */
    data: {
      required: true,
      type: Array,
      default: () => [],
    },
    /**
     * 标题
     */
    title: String,
    /**
     * 表格 列
     */
    cols: {
      required: true,
      type: Array,
      default: () => [],
    },
  },
};
</script>

<style>
</style>